package com.xianzhiOA.service.impl;

import com.github.pagehelper.PageHelper;
import com.xianzhiOA.bean.DateResult;
import com.xianzhiOA.bean.SearchBean;
import com.xianzhiOA.bean.XzPerson;
import com.xianzhiOA.dao.XzPersonMapper;
import com.xianzhiOA.service.XzPersonService;
import com.xianzhiOA.util.ImportUtil;
import com.xianzhiOA.util.PageBean;
import com.xianzhiOA.util.ResumeToHtmlOrPdf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;

/**
 * Created by Administrator on 2017/3/31.
 */
@Service("xzPersonService")
public class XzPersonServiceImpl implements XzPersonService{
    @Autowired
    private XzPersonMapper personMapper;

    //人员插入
    @Override
    public int insert(XzPerson person) {
        return personMapper.insert(person);
    }
    //按id查询
    @Override
    public XzPerson findById(Long id) {
        return personMapper.selectByPrimaryKey(id);
    }
    //按id修改所有属性
    @Override
    public int updateByPrimaryKey(XzPerson record) {
        return personMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(XzPerson person) {
        return personMapper.updateByPrimaryKeySelective(person);
    }

    @Override
    public XzPerson findByIdForComment(Long id) {
        return personMapper.selPersonWithComment(id);
    }

    //分页条件查询
    @Override
    public PageBean<XzPerson> selAll(SearchBean<XzPerson> searchPerson) {
        PageHelper.startPage(searchPerson.getPage(), searchPerson.getRows());
        /*Map<String,Object> map = MakeQ(person);
        if(beginDate!=null)map.put("beginDate",beginDate);
        if(endDate!=null)map.put("endDate",endDate);*/
        List<XzPerson> plist = personMapper.selAll(searchPerson);
        PageBean<XzPerson> pb = new PageBean<>(plist);
        return pb;
    }

    @Override
    public int insertBatch(List<File> excelFiles, Map<String,File> worldFiles, String type,String path) {
        int count = 0;
        List<XzPerson> persons = null;
        if(type.endsWith("zl")){//智联导入
            try {
                persons = ImportUtil.importAllForZL(excelFiles);
                count = makeBatch(persons,worldFiles,path);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(type.endsWith("qc")){//前程导入
            try {
                persons = ImportUtil.importAllForQC(excelFiles);
                count = makeBatch(persons,worldFiles,path);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return count;
    }

    @Override
    public List<DateResult> selectAchievement(Map<String, Object> map) {
        /*Map<String,Object> map = new HashMap<>();
        map.put("empId",1);
        List<DateResult> dd = personMapper.selAchievement(map);
        System.out.println(dd);*/

        return personMapper.selAchievement(map);
    }

    @Override
    public int deletePerson(Long personId) {
        return personMapper.deleteByPrimaryKeyF(personId);
    }

    //批量添加
    private int makeBatch(List<XzPerson> persons,Map<String,File> worldFiles,String path){
        int count = 0;
            if(persons!=null){
                for (XzPerson person : persons) {
                    person.setPersonRecord(2);
                    person.setPersonReception(0);
                    person.setIsdelete(0);
                    Date date = new Date();
                    person.setCreateTime(date);
                    //年龄
                    int oyear = Integer.parseInt(person.getPersonBirth().substring(0,person.getPersonBirth().indexOf("-")));
                    Calendar ca = Calendar.getInstance();
                    int nyear = ca.get(Calendar.YEAR);
                    person.setPersonAge(nyear-oyear);
                    //简历
                    File f = worldFiles.get(person.getPersonName());
                    if(f!=null&&f.exists()){
                        String pdfName = f.getName().substring(0,f.getName().indexOf(".doc"))+".pdf";
                        ResumeToHtmlOrPdf.wordToPDF(path+f.getName(),path+pdfName);
                        f.delete();
                        person.setPersonResume(pdfName);
                    }
                    //检查用户电话是否重复
                    int rs = personMapper.selectF(person);
                    if(rs==0){
                        personMapper.insertSelective(person);
                        count++;
                    }
                }
            }
        return count;
    }


    //分页条件查询条件map
    private Map<String,Object> MakeQ(XzPerson person){
        Map<String,Object> hashMap = new HashMap<>();
        hashMap.put("personName",person.getPersonName());
        hashMap.put("personSex",person.getPersonSex());
        hashMap.put("personBirth",person.getPersonBirth());
        hashMap.put("personAge",person.getPersonAge());
        hashMap.put("personTel",person.getPersonTel());
        hashMap.put("personExp",person.getPersonExp());
        hashMap.put("personEducation",person.getPersonEducation());
        hashMap.put("personJob",person.getPersonJob());
        hashMap.put("personReception",person.getPersonReception());
        hashMap.put("personRecord",person.getPersonRecord());
        hashMap.put("personAudit",person.getPersonAudit());
        hashMap.put("empName",person.getEmpName());
        hashMap.put("empId",person.getEmpId());
        return hashMap;
    }

}
